package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.SCL90;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.IScl90Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-24
 * Time: 16:03
 */

@Slf4j
@Api(tags = "scl90测试问卷接口")
@RestController
@RequestMapping("/scl90")
public class Scl90Controller {

    @Resource
    private IScl90Service iScl90Service;

    //1.新增一条scl90问卷测试记录
    @ApiOperation("新增测试问卷记录")
    @GetMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "scl90选项分值汇总") @RequestParam("data[]") String[]  data) {

        //1.参数校验
        if(data.length <= 0) {
            return AppResult.failed("测试出错了");
        }

        //2.获取当前登录的用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //3.获取data里面的数据进行计算

        double score = 0;                //总分
        double totalAverage = 0;         //总均分
        double positive_Num = 0;         //阳性项目数
        double somatization = 0;         //躯体化
        double forced = 0;               //强迫症状
        double relationship = 0;         //人际关系敏感
        double depression = 0;           //抑郁
        double anxiety = 0;              //焦虑
        double hostile = 0;              //敌对
        double terror = 0;               //恐怖
        double bigotry = 0;              //偏执
        double mentally = 0;             //精神病
        double other = 0;                //其他

        for(int i = 1; i < data.length + 1; i++ ) {

            //计算somatization 躯体化
            if(i == 1 || i == 4 || i == 12 || i == 27 || i == 40 || i == 42 ||i == 48 || i == 49 || i == 52 || i == 53 || i == 56 || i == 58) {
                somatization = somatization + Integer.parseInt(data[i]);
            }
            //计算forced 强迫症状
            if(i == 3 || i == 9 || i == 10 || i == 28 || i == 38 || i == 45 ||i == 46 || i == 51 || i == 55 || i == 65) {
                forced = forced + Integer.parseInt(data[i]);
            }
            //计算relationship 人际关系敏感
            if(i == 6 || i == 21 || i == 34 || i == 36 || i == 37 || i == 41 ||i == 61 || i == 69 || i == 73) {
                relationship = relationship + Integer.parseInt(data[i]);
            }
            //计算depression 抑郁
            if(i == 5 || i == 14 || i == 15 || i == 20 || i == 22 || i == 26 ||i == 29 || i == 30 || i == 31 || i == 32 || i == 54 || i == 71 || i == 79) {
                depression = depression + Integer.parseInt(data[i]);
            }
            //计算anxiety 焦虑
            if(i == 2 || i == 17 || i == 23 || i == 33 || i == 39 || i == 57 ||i == 72 || i == 78 || i == 80 || i == 86) {
                anxiety = anxiety + Integer.parseInt(data[i]);
            }
            //计算hostile 敌对
            if(i == 11 || i == 24 || i == 63 || i == 67 || i == 74 || i == 81) {
                hostile = hostile + Integer.parseInt(data[i]);
            }
            //计算terror 恐怖
            if(i == 13 || i == 25 || i == 47 || i == 50 || i == 70 || i == 75 ||i == 82) {
                terror = terror + Integer.parseInt(data[i]);
            }
            //计算bigotry 偏执
            if(i == 8 || i == 18 || i == 43 || i == 68 || i == 76 || i == 83) {
                bigotry = bigotry + Integer.parseInt(data[i]);
            }
            //计算mentally 精神病
            if(i == 7 || i == 16 || i == 35 || i == 62 || i == 77 || i == 84 ||i == 85 || i == 87 || i == 88 || i == 90) {
                mentally = mentally + Integer.parseInt(data[i]);
            }
            //计算other 其他
            if(i == 19 || i == 44 || i == 59 || i == 60 || i == 64 || i == 66 ||i == 89) {
                other = other + Integer.parseInt(data[i]);
            }

            //判断阳性项目数 positive_Num
            if(i != 91 && Integer.parseInt(data[i]) > 2) {
                positive_Num = positive_Num + 1;
            }
        }

        //4.计算最后的分数

        BigDecimal scoreBigDecimal = new BigDecimal( somatization + forced + relationship + depression + anxiety + hostile + terror + bigotry + mentally + other);
        score =  scoreBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal totalBigDecimal = new BigDecimal( score / 90);
        totalAverage = totalBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();



        BigDecimal somBigDecimal = new BigDecimal(somatization / 12);
        somatization = somBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal forBigDecimal = new BigDecimal(forced / 10);
        forced = forBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal relBigDecimal = new BigDecimal(relationship / 9);
        relationship = relBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal depBigDecimal = new BigDecimal( depression / 13);
        depression = depBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal anxBigDecimal = new BigDecimal( anxiety / 10);
        anxiety = anxBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();



        BigDecimal hosBigDecimal = new BigDecimal( hostile / 6);
        hostile = hosBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal terBigDecimal = new BigDecimal( terror / 7);
        terror = terBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal bigBigDecimal = new BigDecimal( bigotry / 6);
        bigotry = bigBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal menBigDecimal = new BigDecimal( mentally / 10);
        mentally = menBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();


        BigDecimal otherBigDecimal = new BigDecimal( other / 7);
        other = otherBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();





        System.out.println("--------------------");
        System.out.println("总分 = " + score);
        System.out.println("总均分 = " +totalAverage);
        System.out.println("躯体化 = " +somatization);
        System.out.println("强迫症状 = " +forced);
        System.out.println("人际关系敏感 = " +relationship);
        System.out.println("抑郁 = " +depression);
        System.out.println("焦虑 = " +anxiety);
        System.out.println("敌对 = " +hostile);
        System.out.println("恐怖 = " +terror);
        System.out.println("偏执 = " +bigotry);
        System.out.println("精神病 = " +mentally);
        System.out.println("其他 = " +other);
        System.out.println("阳性项目数= " + positive_Num);
        System.out.println("--------------------");


        //5.创建scl90记录对象
        SCL90 scl90 = new SCL90();
        scl90.setUserId(stuUser.getId());
        scl90.setScore(score);
        scl90.setTotalAverage(totalAverage);
        scl90.setPositive_Num(positive_Num);
        scl90.setSomatization(somatization);
        scl90.setForced(forced);
        scl90.setRelationship(relationship);
        scl90.setDepression(depression);
        scl90.setAnxiety(anxiety);
        scl90.setHostile(hostile);
        scl90.setTerror(terror);
        scl90.setBigotry(bigotry);
        scl90.setMentally(mentally);
        scl90.setOther(other);

        //6.调用service
        iScl90Service.create(scl90);

        //7.返回结果
        return AppResult.success();
    }

    //1.新增一条scl90问卷测试记录
    @ApiOperation("新增测试问卷记录")
    @GetMapping("/create1")
    public AppResult test(HttpServletRequest request,
                          @ApiParam(value = "scl90选项分值汇总")@RequestParam("data[]") String[] data) {

        //System.out.println(data);
        //1.参数校验
        if(data.length <= 0) {
            return AppResult.failed("测试出错了");
        }

        System.out.println("进入测试controller");
        System.out.println(data);

        double j = 0;
        double t = 0;
        for(int i = 1; i < data.length + 1; i++) {

            if(i == 1) {
                j = j + Integer.parseInt(data[i]);
            }
            if(i == 2) {
                t = t + Integer.parseInt(data[i]);
            }

        }
        System.out.println("j = " + j);
        System.out.println("t = " + t);
        return null;
    }

    @ApiOperation("查询新增测评记录接口")
    @GetMapping("/select")
    public AppResult<SCL90> selectById(HttpServletRequest request) {

        //1.获取登录信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        SCL90 scl90 = iScl90Service.selectByIdNewResult(stuUser.getId());

        //3.判断查询结果是否为空
        if(scl90 == null || scl90.getDeleteState() == 1) {
           return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //4.返回数据
        return AppResult.success(scl90);
    }


    //根据用户id查询scl90测评记录
    @ApiOperation("查询用户的所有测评记录")
    @GetMapping("/selectAll")
    public AppResult<List<SCL90>> selectByUserId(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<SCL90> scl90List = iScl90Service.selectByUserId(stuUser.getId());

        //3.返回数据
        return AppResult.success(scl90List);

    }


    //根据用户id查询scl90测评记录
    @ApiOperation("查询用户的所有测评记录")
    @GetMapping("/selectAllCon")
    public AppResult<List<SCL90>> selectByUserIdCon(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED);
        }

        //2.调用service
        List<SCL90> scl90List = iScl90Service.selectByUserId(id);

        //3.返回数据
        return AppResult.success(scl90List);
    }

}
